const _ = require('lodash')


class DupeFilter {
    constructor(configuration) {
        this.seen = new Set()
    }

    add(request) {
        this.seen.add(request.url)
    }

    has(request) {
        return this.seen.has(request.url)
    }

    delete(request) {
        this.seen.delete(request.url)
    }

    filter(request) {
        if (!_.defaultTo(request.dupeFilter, true)) {
            return false
        }

        if (this.has(request)) {
            return true
        }

        this.add(request)
        return false
    }
}

module.exports = (configuration) => {
    return new DupeFilter(configuration)
}
